package gui.windows;


import gui.GUIManager;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.Timer;

import room.RoomHandler;

import java.awt.Color;
import java.awt.Frame;

import config.ConfigHandler;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.io.IOException;

public class MainWindow {

	public JFrame frame;
	public Ellipse2D offButton;
	public Ellipse2D miniButton;
	private JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
	/**
	 * Create the application.
	 */
	public MainWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 1050, 650);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		frame.setUndecorated(true);

		try {
			JLabel labelBackground = new JLabel((Icon) new ImageIcon(ImageIO.read(getClass().getResource("/resources/Launcher_Layout_FuckingLarge.png"))));
			frame.setContentPane(labelBackground);
			frame.setBackground(new Color(0,0,0,0));
		} catch (IOException e) {
			e.printStackTrace();
		}
		ConfigHandler.saveCurrentConfig();
		
		//init functions
		
		MoveMouseListener mml = new MoveMouseListener(frame);
		frame.addMouseListener(mml);
		frame.addMouseMotionListener(mml);
		
		tabbedPane.setBackground(Color.BLACK);
		tabbedPane.setBorder(null);
		//tabbedPane.setBackground(new Color(51, 51, 51));
		tabbedPane.setBounds(32, 102, 976, 471);
		//tabbedPane.setOpaque(false);
		frame.getContentPane().add(tabbedPane);
		
		LoginTab homeTab = new LoginTab();
		homeTab.setBorder(null);
		tabbedPane.addTab("Login", null, homeTab, null);
		
		GameListTab gameListTab = new GameListTab();
		gameListTab.setBorder(null);
		GUIManager.mainGameListTab = gameListTab;
		
		TournamentTab tournamentTab = new TournamentTab();
		GUIManager.mainTournamentTab = tournamentTab;
		
		ChatTab chatTab = new ChatTab();
		GUIManager.mainChatTab = chatTab;
		
		FileManagerTab fileManagerTab = new FileManagerTab();
		GUIManager.mainFileManagerTab = fileManagerTab;		
		
		CustomizeTab customizeTab = new CustomizeTab();
		GUIManager.mainCustomizeTab = customizeTab;

        offButton = new Ellipse2D.Float(995, 50, 40, 40);
        MouseAdapter handler = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
                    if (offButton.contains(e.getPoint())) {  	
                        if (frame != null) {
                        	System.exit(1);
                        }
                    }
                }
            }

        };
        frame.addMouseListener(handler);
        frame.addMouseMotionListener(handler);
        
        miniButton = new Ellipse2D.Float(955, 50, 40, 40);
        MouseAdapter handler2 = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
                    if (miniButton.contains(e.getPoint())) {  	
                        if (frame != null) {
                        	frame.setState(Frame.ICONIFIED);
                        }
                    }
                }
            }

        };
        frame.addMouseListener(handler2);
        frame.addMouseMotionListener(handler2);
        
        GUIManager.mainWindow = this;
	}

	public static void startTimer(){
		Timer timer = new Timer(
				1000,
				new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent e) {
						RoomHandler.updateServerDetails();
					}
				});
		timer.start();
	}
	
	public void loadMainTabs(){
		if(tabbedPane.getComponentCount() == 1){
		tabbedPane.addTab("DevPro", null, GUIManager.mainGameListTab, null);
		tabbedPane.addTab("Tournaments", null, GUIManager.mainTournamentTab, null);
		tabbedPane.addTab("Chat", null, GUIManager.mainChatTab, null);
		tabbedPane.addTab("File Manager", null, GUIManager.mainFileManagerTab, null);		
		tabbedPane.addTab("Customize", null, GUIManager.mainCustomizeTab, null);
		tabbedPane.remove(GUIManager.mainHomeTab);
		RoomHandler.sendGameListRequest();
		startTimer();
		}
	}
}
